package com.zqweb.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zqweb.gulimall.product.entity.CategoryEntity;
import com.zqweb.gulimall.product.service.CategoryService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;



/**
 * 商品三级分类
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 18:38:54
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询商品三级分类的树状数据
     */
    @RequestMapping("/list/tree")
    public R listTree(){
        List<CategoryEntity> list = categoryService.listTree();
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改（分类表更改记录时，若相关字段在其他表中有关联记录，也需要对关联表进行相应记录的更新）
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
//		categoryService.updateById(category);
        categoryService.updateDetail(category);
        return R.ok();
    }
    /**
     * 批量修改
     */
    @RequestMapping("/update/drag")
    public R updateBatch(@RequestBody CategoryEntity[] categories){
        categoryService.updateBatchById(Arrays.asList(categories));

        return R.ok();
    }

    /**
     * 删除
     * 方法参数内声明的@RequestBody注解，用于将请求体信息封装到形参变量中，适用于POST请求
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){// 参数信息以数组形式封装
//		categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.logicDeleteByIds(catIds);
        return R.ok();
    }

}
